#include <stdlib.h>
#include <string.h>

typedef struct node_ {
	char tid[6];
	struct node_ *next;
	struct node_ *prev;
} node;

//typedef struct node *tid_list;

void init_node(node *n)
{
	n->tid[0] = '\0';
	n->next = NULL;
	n->prev = NULL;
}

void add_node(node *begin, char *tid)
{
	node *aux = begin->next;
	
	begin->next = (node *)malloc(sizeof(node));
	begin->next->prev = begin;
	begin->next->next = aux;
	memcpy(begin->next->tid, tid, 6);
	//printf("Added node %c%c%c%c\n",tid[0],tid[1],tid[2],tid[3]);
}

void delete_tid(node *begin, char *tid)
{
	node *aux = begin;
	node *n = NULL;

	while ( aux->next != NULL )
	{
		if ( memcmp( aux->next->tid, tid, 6 )==0 )
		{
			// Delete aux->next
			//printf("To delete node %c%c%c%c\n",tid[0],tid[1],tid[2],tid[3]);
			n = aux->next->next;
			if (n) n->prev = aux;
			free(aux->next);
			aux->next = n;
			//printf("Deleted node %c%c%c%c\n",tid[0],tid[1],tid[2],tid[3]);
			break;
		} else {
			// Try next node
			aux = aux->next;
		}
	}
}

/*
void delete_node(node **target)
{
	//printf("Delete node 0x%x\n", (*target));
	
	if ( target && (*target) )
	{
		(*target)->prev->next = (*target)->next;
		if ( (*target)->next ) (*target)->next->prev = (*target)->prev;
		free((*target));
	}
}
*/
